import axios from 'axios';
import store from '@/store/index';

import qs from 'qs'
axios.defaults.timeout = 30000;
import { Message } from 'element-ui'
// axios.defaults.baseURL ='/flower-server';//开发
// axios.defaults.baseURL ='www.qmptued.cn';//上线



// request拦截器
// service.interceptors.request.use(
//   config => {
//       config.headers['Content-Type'] = 'application/json;charset=UTF-8'
//   },
//   error => {
//       Promise.reject(error)
//   }
// )
//http request 拦截器
axios.interceptors.request.use(
  config => {
    // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
    // config.data = JSON.stringify(config.data);
    config.headers = {
      'Content-Type':'application/json'
    }

    if(window.localStorage.getItem('rabate_token')) {
        config.headers['access-token'] =  window.localStorage.getItem('rabate_token')

    }
    // if(token){
    //   config.params = {'token':token}
    // }
    return config;
  },
  error => {
    return Promise.reject(err);
  }
);


//http response 拦截器
axios.interceptors.response.use(
  response => {
    if(response.data.status ==-10086){
      // router.push({
      //   path:"/login",
      //   querry:{redirect:router.currentRoute.fullPath}//从哪个页面跳转
      // })
      // window.location.href='/login'
    }
    // debugger
      if(response.data.status === 0) return Promise.reject(response.data)
      return response;
  },
  error => {
    Message({
      message: error.response.data.msg,
      type: 'error',
      duration: 3 * 1000
    })
    return Promise.reject(error)
  }
)


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    })
    .then(response => {
      resolve(response.data);
    })
    .catch(err => {
      reject(err)
    })
  })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

 export function post(url,data = {}, type = true){
   return new Promise((resolve,reject) => {
      //  axios.post(url,type ? qs.stringify(data) : data)
       axios.post(url,type ? JSON.stringify(data) : data)
          .then(response => {
            resolve(response.data);
          },err => {
            reject(err)
          })
   })
 }

 /**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}

 /**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}
 /**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function del(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.delete(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}

